import Card
# -*- coding: utf-8 -*-
from pygame.locals import *
from NetPackets import *
                             
class Card:

    def __init__(self, frontImage, backImage, x=0, y=0, id=0, connection=None):
        self.connection = connection
        self.bimg = backImage
        self.fimg = frontImage
        self.img = backImage
        self.side = False
        self.rect = self.img.get_rect()
        self.rect.left = x
        self.rect.top = y
        self.child = None
        self.parent = None
        self.selected = 0
        self.rotated = False
        self.id = id
        self.highlighted = False
        if self.connection:
            sentPacket(COMMAND_CARD_ADD, (self.id, self.rect.left, self.rect.top), self.connection)

    def flip(self):
        """Flip the card """
        self.setSide(not self.side)

    def backSide(self):
        """Flip the card to back hidden side """
        self.setSide(False)

    def frontSide(self):
        """Flip the card to front side """
        self.setSide(True)

    def setSide(self, side):
        if side:
            self.img = self.fimg
        else:
            self.img = self.bimg
        self.side = side
        if self.connection:
            sentPacket(COMMAND_CARD_FLIP, (self.id, int(side)), self.connection)
       
    def move(self, dx, dy):
        self.rect.x += dx
        self.rect.y += dy
        if self.child:
            self.child.move(dx, dy)
        if self.connection:
            sentPacket(COMMAND_CARD_MOVE, (self.id, self.rect.left, self.rect.top), self.connection)

    def move_abs(self, x, y):
        self.rect.x = x
        self.rect.y = y
        if self.connection:
            sentPacket(COMMAND_CARD_MOVE, (self.id, self.rect.left, self.rect.top), self.connection)

    def draw(self, surface):
        print type(surface)

        surface.blit(self.img, self.rect.topleft)

    def getInfo(self):
        return (self.side, self.rect.topleft, self.child, self.parent, self.selected)

    def setInfo(self, info):
        self.setSide(info[0])
        self.rect.topleft = info[1]
        self.child = info[2]
        self.parent = info[3]
        self.selected = info[4]            

    def turn(self):
        self.setTurn(not self.rotated)

    def setTurn(self, turn):
        if self.rotated == turn:
            return
        if turn == True:
            self.img = pygame.transform.rotate(self.img, 90)
            self.rect.width, self.rect.height = self.rect.height, self.rect.width
            self.rotated = True
        else:
            self.img = pygame.transform.rotate(self.img, -90)
            self.rect.width, self.rect.height = self.rect.height, self.rect.width
            self.rotated = False
        if self.connection:
            sentPacket(COMMAND_CARD_ROTATE, (self.id, int(self.rotated)), self.connection)

    def setHighlight(self, bool):
#        if self.highlighted == False:
        self.highlighted = bool
        print "highlight card", self.id


class CardGL(Card):

    def __init__(self, frontImage, backImage, x=0, y=0, id=0, connection=None, texture=None):
        Card.__init__(self, frontImage, backImage, x, y, id, connection)
        self.texture = texture

    def draw(self):
        self.img.draw(position=(self.rect.left, self.rect.top), origin=(0,0))
